package org.example;

import org.junit.Test;

import java.util.Objects;
import java.util.concurrent.*;

public class DemoTest3 {
    @Test
    public void testCompletableGet() throws InterruptedException, ExecutionException {
        CompletableFuture<String> cp1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "商品A";
        });

        // getNow方法测试
        System.out.println(cp1.getNow("商品B"));

        //join方法测试
//        CompletableFuture<Integer> cp2 = CompletableFuture.supplyAsync((() -> 1 / 0));
        CompletableFuture<Integer> cp2 = CompletableFuture.supplyAsync(() -> {
            if (true) {
                throw new RuntimeException("业务2异常");
            }
            return 22;
        });
        try {
            System.out.println(cp2.join());
        } catch (Exception e) {
            Throwable cause = e.getCause();
            System.out.println(cause);
            e.printStackTrace();
        }

        System.out.println("-----------------------------------------------------");

        //get方法测试
        CompletableFuture<Integer> cp3 = CompletableFuture.supplyAsync((() -> 1 / 0));
        try {
            System.out.println(cp3.get());
        } catch (Exception e) {
            Throwable cause = e.getCause();
            System.out.println(cause);
            e.printStackTrace();
        }
    }

}
